package com.cnbm.wcsbasic.protocol.netty.io.syn;


import com.cnbm.wcsbasic.protocol.netty.io.common.CaffeineUtil;
import com.cnbm.wcsbasic.protocol.netty.io.common.Package;

/**
 * @Desc: "MES端 发送远程指令列表"
 * @Author: caixiang
 * @DATE: 2021/8/17 14:14
 */
public class SendedList {
//    private static HashMap<String, AsyncFuture<MBPackage>> list = new HashMap<>();
//    public static synchronized AsyncFuture<MQMessage> get(String transitionId){
//        return list.get(transitionId);
//    }
//    public static synchronized void put(String transitionId,AsyncFuture<MQMessage> asyncFuture){
//        list.put(transitionId,asyncFuture);
//    }

    public static synchronized AsyncFuture<Package> add(String transitionId, Package PackageRequest) {

        AsyncFuture<Package> objectAsyncFuture = new AsyncFuture<>(PackageRequest);
        //list.put(transitionId,objectAsyncFuture);
        CaffeineUtil.put(transitionId,objectAsyncFuture);
        return objectAsyncFuture;
    }

    //如果超时了，那么让外部把这个key 给清除掉，防止 list 过大
    public static synchronized void remove(String transitionId) {
        //list.remove(transitionId);
        CaffeineUtil.remove(transitionId);
    }

    public static synchronized Boolean set(String transitionId, Package message) {
        //AsyncFuture<MBPackage> mqMessageAsyncFuture = list.get(transitionId);
        AsyncFuture<Package> mqMessageAsyncFuture = (AsyncFuture<Package>)CaffeineUtil.get(transitionId);
        if(mqMessageAsyncFuture == null){
            return false;
        }
        mqMessageAsyncFuture.done(message);
        //清除 ，防止这个hashMap过大。
        CaffeineUtil.remove(transitionId);
        return true;
    }
}
